"""

.. module:: service_test
.. moduleauthor:: @author: 

*Description*:

This module is the unit test for the Service module.

*Constants*:

.. seealso:: class :class:`runtime.service.Service`
"""

# Imports
#
import unittest
import logging

from datastore.resource import *
from datastore.spaces import *
from datastore.uri import *
from runtime.service import *

# Constants
#
logger = logging.getLogger('yarra')
hdlr = logging.StreamHandler()
formatter = logging.Formatter('%(levelname)s %(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(logging.INFO)

# Classes
#
class ServiceTest(unittest.TestCase):
    
    def setUp(self):
        self.spaces = Spaces()
        # standard definitions to pass validation in spaces.put()
        res1 = Resource(type='term:base/runtime/definition')
        self.spaces.put(Uri('term:base/runtime/definition'), res1, 
                        warning=False)
        res1 = Resource(type='term:base/runtime/definition')
        self.spaces.put(Uri('term:base/datastore/resource/type'), res1,
                        warning=False)
        res1 = Resource(type='term:base/runtime/definition')
        self.spaces.put(Uri('dc:title'), res1)
        res1 = Resource(type='term:base/runtime/definition')
        self.spaces.put(Uri('dc:description'), res1)
        res1 = Resource(type='term:base/runtime/definition')
        self.spaces.put(Uri('term:base/runtime/service'), res1)

    def test_lifecycle(self):
        """
        [Spaces] Watch with callback function
        """
        
        service_def_uri = Uri('internal:task/base/services/test_service')
        service_def = Resource(type='term:base/runtime/service')
        service_def.set_metadata(Uri('dc:title'), 'A Test Service')
        self.spaces.put(service_def_uri, service_def)
        NodeLogger.load()
        service = Service(self.spaces, None, service_def_uri)
        service.start()
        
        self.spaces.delete(service_def_uri)


if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()